package ui;



import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.VetoableChangeListener;
import java.util.Vector;

public class ShowInterface {
    JFrame jf=new JFrame("用户管理");
    Object[] titles={"姓名","年龄","性别"};
    Object[][] data={{"张三",20,"男"},{"李四",18,"男"},{"王五",20,"男"},{"刘凯",18,"女"}};
    private Vector titlesV=new Vector();
    private Vector<Vector> dataV=new Vector<>();


    public void init(){
        for (int i = 0; i < titles.length; i++) {
            titlesV.add(titles[i]);
        }
        for (int i = 0; i < data.length; i++) {
            Vector t=new Vector<>();
            for (int j = 0; j < data[i].length; j++) {
                t.add(data[i][j]);
            }
            dataV.add(t);
        }
        //JTable jTable=new JTable(new MyTableModel());
        DefaultTableModel model=new DefaultTableModel(dataV,titlesV);
        JTable jTable=new JTable(model);
        //创建按钮
        JButton addRow=new JButton(("添加"));
        JButton deleteRow=new JButton("删除");
        addRow.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                model.addRow(new Object[]{"牛逼",1000,"中性"});
            }
        });
        deleteRow.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int selectedRow = jTable.getSelectedRow();
                model.removeRow(selectedRow);

            }
        });
        JPanel panel=new JPanel();
        panel.add(addRow);
        panel.add(deleteRow);
        jf.add(panel, BorderLayout.SOUTH);
        //只能选择单行
        jTable.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        //设置列宽
        TableColumn column_1=jTable.getColumn(titles[0]);
        column_1.setMinWidth(40);
        TableColumn column_3=jTable.getColumn(titles[2]);
        column_3.setMinWidth(40);
        jf.add(new JScrollPane(jTable));
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.pack();
        jf.setVisible(true);


    }
  /*  private class MyTableModel extends AbstractTableModel{

        @Override
        public int getRowCount() {
            return dataV.size();
        }

        @Override
        public int getColumnCount() {
            return titlesV.size();
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            return dataV.get(rowIndex).get(columnIndex);
        }
       public String getColumnName(int column){
            return (String) titlesV.get(column);
       }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return true;
        }
    }*/
    public static void main(String[] args) {
        new ShowInterface().init();



    }
}
